package com.aaa.rsatest;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtils {

    private static volatile SPUtils sSPUtils;
    private final SharedPreferences mSp;

    private SPUtils(Context context) {
        mSp = context.getSharedPreferences("sp", Context.MODE_PRIVATE);
    }

    public static SPUtils getInstance(Context context) {
        if (sSPUtils == null) {
            synchronized (SPUtils.class) {
                if (sSPUtils == null) {
                    sSPUtils = new SPUtils(context);
                }
            }
        }
        return sSPUtils;
    }

    // java代码生成的密钥对
    private static final String private_key = "private_key";
    private static final String public_key = "public_key";

    public boolean setPrivateKey(String value) {
        return mSp.edit().putString(private_key, value).commit();
    }

    public String getPrivateKey() {
        return mSp.getString(private_key, "");
    }

    public boolean setPublicKey(String value) {
        return mSp.edit().putString(public_key, value).commit();
    }

    public String getPublicKey() {
        return mSp.getString(public_key, "");
    }

    // 从私钥文件获取的私钥，从公钥文件获取的公钥
    private static final String private_key2 = "private_key2";
    private static final String public_key2 = "public_key2";

    public boolean setPrivateKey2(String value) {
        return mSp.edit().putString(private_key2, value).commit();
    }

    public String getPrivateKey2() {
        return mSp.getString(private_key2, "");
    }

    public boolean setPublicKey2(String value) {
        return mSp.edit().putString(public_key2, value).commit();
    }

    public String getPublicKey2() {
        return mSp.getString(public_key2, "");
    }

    // 从证书文件获取的公钥
    private static final String public_key3 = "public_key3";

    public boolean setPublicKey3(String value) {
        return mSp.edit().putString(public_key3, value).commit();
    }

    public String getPublicKey3() {
        return mSp.getString(public_key3, "");
    }
}
